package dao;

import hibernateUtils.HibernateUtil;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import beans.AdministracionBean;
import beans.SucursalBean;

public class HibernateAdministracionDAO {
	private static HibernateAdministracionDAO instancia;
	private static SessionFactory sf;

	private HibernateAdministracionDAO() {

	}

	public static HibernateAdministracionDAO getInstancia() {
		if (instancia == null) {
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateAdministracionDAO();
		}
		return instancia;
	}

	public void grabarAdministracion(AdministracionBean fb) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(fb);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public AdministracionBean buscarAdministracion(AdministracionBean fb) {
		Session session = sf.openSession();
		AdministracionBean f = (AdministracionBean) session.get(
				AdministracionBean.class, fb.getId());
		session.close();
		return f;
	}

	public List<AdministracionBean> buscarAdministracionXSucursal(
			SucursalBean sb) {
		Session session = sf.openSession();
		//session.beginTransaction();
		@SuppressWarnings("unchecked")
		List<AdministracionBean> administraciones = (List<AdministracionBean>) session
				.createQuery(
						"from AdministracionBean where idSucursal = :idSuc")
				.setParameter("idSuc", sb.getIdSucursal()).list();
		session.close();
		return administraciones;
	}

	public void updateAdministracion(AdministracionBean admb) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.update(admb);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public void mergeAdministracion(AdministracionBean admb) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.merge(admb);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
}
